<?php

namespace Frieza\Modules\Photo\Extensions;

use Encore\Admin\Admin;

class SetHot
{
    protected $id;
    /**
     * @var false
     */
    private $isHidden;

    public function __construct($id, $isHidden)
    {
        $this->id = $id;
        $this->isHidden = $isHidden;
    }

    protected function script()
    {
        return <<<SCRIPT

$('.operate-sethot').on('click', function () {
    var _this = $(this);
    let noteId = $(this).data('id');
    let type = 'sethot';
    let params = {note_id:noteId, type:type}
    $.post(url, params , function (e) {
        if(e.code == 1) {
            _this.addClass('hidden');
            _this.siblings('.operate-cancelhot').removeClass('hidden');  
            toastr.success(e.msg, null, {
                timeOut : 1000
            });  
        } else {
            toastr.error(e.msg, null, {
                timeOut : 1000
            });
        }
    });
});

SCRIPT;
    }

    public function render()
    {
        Admin::script($this->script());
        $hidden = $this->isHidden ? "hidden" : "";
        return "<a class='btn btn-xs btn-success fa fa-check operate-sethot {$hidden}' data-id='{$this->id}'>推荐至热门</a>";
    }

    public function __toString()
    {
        return $this->render();
    }
}
